package irri.soils.ssnm;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

public class ActivityT extends SSNM implements View.OnClickListener, 
	OnItemSelectedListener, TextWatcher{
	
	private View button1;
	private int option;
	private Bundle b;
	private SQLiteDatabase myDB;
	private List<Integer> GWList;
	private List<Integer> GWList2;
	private Spinner spinner2;
	private Spinner spinner;
	private EditText editText1;
	private String varName;
	private int GW;
	private int varPosition = 0;

	public List<String> selectAll() {
      List<String> list = new ArrayList<String>();
      list.add("Select a variety");
      Cursor cursor = myDB.rawQuery("select var1, variety, var3, GW " +
      		"from tblvar WHERE type = '1' ORDER BY var1, variety;", null);
      GWList = new ArrayList<Integer>();
      GWList.add(-1);
      if (cursor.moveToFirst()) {
         do {
            list.add(cursor.getString(0)+cursor.getString(1)+cursor.getString(2));
            GWList.add(cursor.getInt(3));
         } while (cursor.moveToNext());
         list.add(get("variety22"));
         GWList.add(-1);
      }
      if (cursor != null && !cursor.isClosed()) {
         cursor.close();
      }
      return list;
	}
	
	public List<String> selectAll2() {
	      List<String> list = new ArrayList<String>();
	      list.add("Select a variety");
	      Cursor cursor = myDB.rawQuery("select var1, variety, var3, GW " +
	      		"from tblvar WHERE type = '0' ORDER BY var1, variety;", null);
	      GWList2 = new ArrayList<Integer>();
	      GWList2.add(-1);
	      if (cursor.moveToFirst()) {
	         do {
	            list.add(cursor.getString(0)+cursor.getString(1)+cursor.getString(2));
	            GWList2.add(cursor.getInt(3));
	         } while (cursor.moveToNext());
	         list.add(get("variety22"));
	         GWList2.add(-1);
	      }
	      if (cursor != null && !cursor.isClosed()) {
	         cursor.close();
	      }
	      return list;
		}
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.g);
        setCustomTitle();
        b = this.getIntent().getExtras();
        int i = b.getInt("season");
        int crop = b.getInt("crop");
        TextView tv = (TextView) findViewById(R.id.question);
        if(crop<3)
        tv.setText(tv.getText()+" "+(i==1 ? "wet" : "dry")+" season?");
        else
        tv.setText(tv.getText()+" "+(i==1 ? "early wet" : 
        	(i==2 ? "late wet" : "dry"))+" season?");
        
        
        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(this);
        button1.setEnabled(false);
        editText1 = (EditText) findViewById(R.id.editText1);
        editText1.addTextChangedListener(this);
        
        myDB = null;
        
        try {
        myDB = this.openOrCreateDatabase("SSNM2", MODE_WORLD_WRITEABLE, null);
        spinner = (Spinner) findViewById(R.id.spinner1);
		ArrayAdapter<Object> adapter = new ArrayAdapter<Object>(this, android.R.layout.simple_spinner_item, 
	    		selectAll().toArray());
	    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    spinner.setAdapter(adapter);
	    spinner.setOnItemSelectedListener(this);
	    
	    spinner2 = (Spinner) findViewById(R.id.spinner2);
		ArrayAdapter<Object> adapter2 = new ArrayAdapter<Object>(this, android.R.layout.simple_spinner_item, 
	    		selectAll2().toArray());
	    
	    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	    spinner2.setAdapter(adapter2);
	    spinner2.setOnItemSelectedListener(this);
        }catch(Exception ex){
        	
        }finally {
            if(myDB != null) myDB.close();
        }
    }

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.button1:
			int i = b.getInt("CE");
			Intent intent = new Intent(ActivityT.this,
					(i==1 ? ActivityH.class : (varName.equals(get("variety22"))
							? ActivityI.class : ActivityJ.class))
				);
			if(i==2) b.remove("SE");
			b.putInt("variety", option);
			b.putString("varName", varName);
			b.putString("otherVar", editText1.getText().toString());
			b.putInt("varPosition", varPosition);
			if(!varName.equals(get("variety22"))) b.putInt("GW", GW);
			intent.putExtras(b);
			startActivity(intent);
			finish();
			break;
		}
	}
	
	@Override
	public void onBackPressed() {
		Intent intent = new Intent(ActivityT.this, ActivityF.class);
		intent.putExtras(b);
		startActivity(intent);
		finish();
	}

	@Override
	public void onItemSelected(AdapterView<?> view, View selected, int position,
			long count) {
		varName = (String) ((Spinner)view).getItemAtPosition(position);
		if(varName.equals(get("variety22"))){
			editText1.setVisibility(View.VISIBLE);
			button1.setEnabled(!editText1.getText().toString().equals(""));
		}
		else if(!varName.equals("Select a variety")){
			editText1.setVisibility(View.GONE);
			button1.setEnabled(true);
			if(option==1) GW = GWList.get(position);
			else GW = GWList2.get(position);
		}
		varPosition = position;
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		
	}
	
	public void afterTextChanged(Editable s) {
		button1.setEnabled(!editText1.getText().toString().equals(""));
    }
    	
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
    }
        
    public void onTextChanged(CharSequence s, int start, int before, int count) {
            
    }
}